package com.szh.taobaounion.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.szh.taobaounion.R;
import com.szh.taobaounion.model.domain.HomePagerContent;
import com.szh.taobaounion.model.domain.IBaseInfo;
import com.szh.taobaounion.model.domain.ILinearItemInfo;
import com.szh.taobaounion.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LinearItemContentAdapter extends RecyclerView.Adapter<LinearItemContentAdapter.InnerHolder> {
    List<ILinearItemInfo > mData = new ArrayList<>();
    private onItemClickListener mItemClickListener=null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_linear_goods_content, parent, false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        ILinearItemInfo  dataBean = mData.get(position);
        //设置数据
        holder.setData(dataBean);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(dataBean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setmData(List<? extends ILinearItemInfo > contents) {
        mData.clear();
        mData.addAll(contents);
        notifyDataSetChanged();
    }

    public void addData(List<? extends ILinearItemInfo> contents) {
        //添加之前拿到原来的size
        int oldSize = mData.size() - 1;
        mData.addAll(contents);
          //更新Ui
          notifyItemRangeChanged(oldSize,contents.size());
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.goods_cover)
        public ImageView goods_cover;
        @BindView(R.id.goods_title)
        public TextView goods_title;
        @BindView(R.id.goods_off_price)
        public TextView goods_off_price;  //省多少钱

        @BindView(R.id.goods_after_off_price)
        public TextView finalPriceTv;      //优惠后价格

        @BindView(R.id.before_price)
        public TextView beforePriceTv;

        @BindView(R.id.goods_pay_num)
        public TextView payNum;//购买人数
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(ILinearItemInfo  dataBean) {
            Context context = itemView.getContext();
            ViewGroup.LayoutParams layoutParams = goods_cover.getLayoutParams();
            int width = layoutParams.width;
            int height = layoutParams.height;
            int coverSize=(width>height?width:height)/2;
            goods_title.setText(dataBean.getTitle());
           // LogUtils.d(this, "url------>" + dataBean.getPict_url());
            String coverPath = UrlUtils.getCoverPath(dataBean.getCover(), coverSize);
           // LogUtils.d(this,"coverPath"+coverPath);
            Glide.with(context).load(coverPath).into(goods_cover);
            long couponAmount = dataBean.getCouponAmount();//省多少钱
            String finalPrice = dataBean.getFinalPrise();//原价
            float resultPrice = Float.parseFloat(finalPrice )- couponAmount;
//            LogUtils.d(this,"resultPrice---->"+resultPrice);
//            LogUtils.d(this,"zk_final_price---->"+finalPrice);
            goods_off_price.setText(String.format(context.getString(R.string.goods_after_price),couponAmount ));
            finalPriceTv.setText(String.format("%.2f",resultPrice));
            beforePriceTv.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            beforePriceTv.setText(String.format(context.getString(R.string.goods_before_price),finalPrice));
            long volume = dataBean.getVolume();
            payNum.setText(String.format(context.getString(R.string.goods_pay_num),volume));
        }
    }
    public void setonItemClickListener(onItemClickListener listener){
      this.mItemClickListener=listener;
    }
    public interface onItemClickListener{
        void onItemClick(IBaseInfo item);
    }
}
